package ftp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.channels.FileChannel;

class Chater implements Runnable {

	private Socket socket;
	String filePath = "/ftp";

	Chater(Socket socket) {
		super();
		this.socket = socket;
	}

	private void chat() throws Exception {

		OutputStream out = socket.getOutputStream();
		BufferedOutputStream bout = new BufferedOutputStream(out, 64);
		InputStream in = socket.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		File f = new File(filePath);
		String response = "";
		int count = 1;
		while (true) {
			if (count == 1) {
				response = "pls try to enter these command: ls, download file,rm file\n";
				out.write(response.getBytes());
			}

			String line = reader.readLine();

			if (line.equals("ls")) {
				response = arrToString(f.list());
			} else if (line.startsWith("rm")) {
				deleteFile(line.split(" ")[1]);
				response = arrToString(f.list());
			} else if (line.startsWith("download")) {
				String name = line.split(" ")[1];
				// new Thread(new CopyThread(name, filePath,out)).start();
				in = new FileInputStream(filePath + "/" + name);
				byte[] b = new byte[10 * 1024];
				in.read(b);
				response = "file" + new String(b) + "\n";
				System.out.println(response);

			} else if (line.equals("bye"))
				break;

			out.write(response.getBytes());

			count++;
		}
		out.close();
		socket.close();
	}

	@Override
	public void run() {
		try {
			chat();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String arrToString(String[] arr) {
		String str = "";
		for (String s : arr)
			str += s + " ";
		return str + "\n";
	}

	private void deleteFile(String fileName) {
		File f = new File(filePath + "/" + fileName);
		if (f.exists()) {
			f.delete();
		}
	}
}

class CopyThread implements Runnable {

	private String fileName;
	private String filePath;
	private OutputStream out;

	public CopyThread(String fileName, String filePath, OutputStream out) {
		super();
		this.fileName = fileName;
		this.filePath = filePath;
		this.out = out;
	}

	@Override
	public void run() {
		InputStream in = null;
		BufferedInputStream bin = null;
		BufferedOutputStream bout = null;
		try {
			in = new FileInputStream(filePath + "/" + fileName);
			bin = new BufferedInputStream(in, 64);
			bout = new BufferedOutputStream(out, 64);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		int m = 0;
		try {
			while ((m = bin.read()) != -1) {
				bout.write(m);
			}
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

class Copy implements Runnable {

	private String fileName;
	private String filePath;

	public Copy(String fileName, String filePath) {
		super();
		this.fileName = fileName;
		this.filePath = filePath;
	}

	@Override
	public void run() {
		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;

		try {
			fi = new FileInputStream(new File(filePath + "/" + fileName));
			fo = new FileOutputStream(new File(fileName));
			in = fi.getChannel();
			out = fo.getChannel();
			in.transferTo(0, in.size(), out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();

			}

		}
	}

}
